package com.winning.pmph.ctrl;

import com.winning.pmph.entity.Role;
import com.winning.pmph.entity.Task;
import com.winning.pmph.entity.TaskItem;
import com.winning.pmph.entity.User;
import com.winning.pmph.service.RoleService;
import com.winning.pmph.service.TaskItemService;
import com.winning.pmph.service.TaskService;
import com.winning.pmph.service.UserService;
import com.winning.pmph.utils.PMPHAppUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "工作台")
@RestController
@RequestMapping("/pmph/action/work")
public class WorkBenchCtrl {

    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private TaskItemService taskItemService;
    @Resource
    private TaskService taskService;

    @ApiOperation(value = "selectRemindPageNum", notes = "工作台查询我的审核提醒数据接口")
    @GetMapping("selectRemindPageNum")
    public  Map<String, Object> selectRemindPageNum(String system) throws Exception {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        Map<String, Object> map = new HashMap<>();
        String knowledgeSql = "select *  from task_item ti LEFT JOIN task t  on t.id=ti.task_id where  t.system_type='knowledge' and ti.editor='" + currentUserEnName + "' and ti.deleted=" + TaskItem.NO_DELETED;
        String calculateSql = "select *  from task_item ti LEFT JOIN task t  on t.id=ti.task_id where  t.system_type='calc' and ti.editor='" + currentUserEnName + "' and ti.deleted=" + TaskItem.NO_DELETED;
        String flowChartSql = "select *  from task_item ti LEFT JOIN task t  on t.id=ti.task_id where  t.system_type='flowchart' and ti.editor='" + currentUserEnName + "' and ti.deleted=" + TaskItem.NO_DELETED;

        switch (system) {
            case "un_commit": {
                knowledgeSql += " and ti.status=" + TaskItem.STATUS_NOT_COMMIT;
                int knowledgeNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
                calculateSql += " and ti.status=" + TaskItem.STATUS_NOT_COMMIT;
                int calculateNum = taskItemService.getBaseMapper().executeMultiSelect(calculateSql).size();
                flowChartSql += " and ti.status=" + TaskItem.STATUS_NOT_COMMIT;
                int flowChartNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();
                map.put("knowledgeTaskItemNum", knowledgeNum);
                map.put("calculateTaskItemNum", calculateNum);
                map.put("flowChartTaskItemNum", flowChartNum);
                break;
            }
            case "reject": {
                knowledgeSql += " and ti.status=" + TaskItem.STATUS_REJECT;
                int knowledgeNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
                calculateSql += " and ti.status=" + TaskItem.STATUS_REJECT;
                int calculateNum = taskItemService.getBaseMapper().executeMultiSelect(calculateSql).size();
                flowChartSql += " and ti.status=" + TaskItem.STATUS_REJECT;
                int flowChartNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();
                map.put("knowledgeTaskItemNum", knowledgeNum);
                map.put("calculateTaskItemNum", calculateNum);
                map.put("flowChartTaskItemNum", flowChartNum);
                break;
            }
            case "press": {
                knowledgeSql += " and (ti.status != " + TaskItem.STATUS_RELEASED + " && ti.status = " + TaskItem.STATUS_NOT_COMMIT + " ||ti.status = " + TaskItem.STATUS_REJECT + ") and press = 1";
                int knowledgeNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
                calculateSql += " and (ti.status != " + TaskItem.STATUS_RELEASED + " && ti.status = " + TaskItem.STATUS_NOT_COMMIT + " ||ti.status = " + TaskItem.STATUS_REJECT + ") and press = 1";
                int calculateNum = taskItemService.getBaseMapper().executeMultiSelect(calculateSql).size();
                flowChartSql += " and (ti.status != " + TaskItem.STATUS_RELEASED + " && ti.status = " + TaskItem.STATUS_NOT_COMMIT + " ||ti.status = " + TaskItem.STATUS_REJECT + ") and press = 1";
                int flowChartNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();
                map.put("knowledgeTaskItemNum", knowledgeNum);
                map.put("calculateTaskItemNum", calculateNum);
                map.put("flowChartTaskItemNum", flowChartNum);
                break;
            }
            case "overTime": {
                String klgSql = "select  *  from task_item  where task_id in (select id from task where system_type='knowledge' and deleted ='0' and  end_date > (NOW() + INTERVAL -3 DAY) and status=1 )and status in (0,3) and deleted ='0' and editor='" + currentUserEnName + "'";
                String calcSql = "select  * from task_item  where task_id in (select id from task where system_type='calc' and deleted ='0' and  end_date > (NOW() + INTERVAL -3 DAY) and status=1 )and status in (0,3) and deleted ='0' and editor='" + currentUserEnName + "'";
                String fChartSql = "select  * from task_item  where task_id in (select id from task where system_type='flowchart' and deleted ='0' and  end_date > (NOW() + INTERVAL -3 DAY) and status=1 )and status in (0,3) and deleted ='0' and editor='" + currentUserEnName + "'";
                int knowledgeNum = taskItemService.getBaseMapper().executeMultiSelect(klgSql).size();
                int calculateNum = taskItemService.getBaseMapper().executeMultiSelect(calcSql).size();
                int flowChartNum = taskItemService.getBaseMapper().executeMultiSelect(fChartSql).size();

                map.put("knowledgeTaskItemNum", knowledgeNum);
                map.put("calculateTaskItemNum", calculateNum);
                map.put("flowChartTaskItemNum", flowChartNum);
                break;
            }
            default: {
                throw new Exception("未知的知识体系类型：" + system);
            }
        }


       return map;
    }

    @ApiOperation(value = "selectTaskNum", notes = "工作台查询我的任务数据接口")
    @GetMapping("selectTaskNum")
    public Map<String, Object> selectTaskNum() {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        Map<String, Object> map = new HashMap<>();
        String knowledgeSql = "select * from task where created_by='" + currentUserEnName + "' and system_type='knowledge' and status = " + Task.STATUS_STARTED;
        String calculateSql = "select * from task where created_by='" + currentUserEnName + "' and system_type='calc' and status = " + Task.STATUS_STARTED;
        String flowChartSql = "select * from task where created_by='" + currentUserEnName + "' and system_type='flowchart' and status = " + Task.STATUS_STARTED;
        int knowledgeCountNum = taskService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
        int calculateCountNum = taskService.getBaseMapper().executeMultiSelect(calculateSql).size();
        int flowChartCountNum = taskService.getBaseMapper().executeMultiSelect(flowChartSql).size();

        map.put("knowledgeTaskNum", knowledgeCountNum);
        map.put("calculateTaskNum", calculateCountNum);
        map.put("flowChartTaskNum", flowChartCountNum);
        return map;
    }

    @ApiOperation(value = "selectEditMessageTable", notes = "工作台查询我的编辑数据接口")
    @GetMapping("selectEditMessageTable")
    public List<TaskItem> selectEditMessageTable(String system) throws Exception {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        String sql = "";
        switch (system) {
            case "newTask": {
                sql = "select * from task where id in(select task_id from task_item where status!=" + TaskItem.STATUS_RELEASED + " and editor='" + currentUserEnName + "') and status =" + Task.STATUS_STARTED + " order by updated_time desc";
                break;
            }
            case "rejectTask": {
                sql = "select * from  task_item where status=" + TaskItem.STATUS_REJECT + " and editor='" + currentUserEnName + "' order by updated_time desc";
                break;
            }
            case "pressTask": {
                sql = "select * from  task_item where status!=" + TaskItem.STATUS_RELEASED + " and editor='" + currentUserEnName + "' and press = 1 and (status = " + TaskItem.STATUS_NOT_COMMIT + " || status = " + TaskItem.STATUS_REJECT + ") order by updated_time desc";
                break;
            }
            default: {
                throw new Exception("未知的知识体系类型：" + system);
            }
        }
        List<TaskItem> taskItems = taskItemService.getBaseMapper().executeMultiSelect(sql);
        return taskItems;

    }

    @ApiOperation(value = "selectSum", notes = "工作台查询环形比数据接口")
    @GetMapping("selectSum")
    public Map<String, Object> selectSum() {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        Map<String, Object> map = new HashMap<>();
        String knowledgeSumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + "  and t.system_type='knowledge' and ti.deleted=" + TaskItem.NO_DELETED;
        String calculateSumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + "  and t.system_type='calc' and ti.deleted=" + TaskItem.NO_DELETED;
        String flowChartSumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + "  and t.system_type='flowchart' and ti.deleted=" + TaskItem.NO_DELETED;
        int knowledgeSum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSumSql).size();
        int calculateSum = taskItemService.getBaseMapper().executeMultiSelect(calculateSumSql).size();
        int flowChartSum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSumSql).size();
        String knowledgeUnfinishedNumSql = "select * from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  (ti.status=" + TaskItem.STATUS_NOT_COMMIT + "  or ti.status=" + TaskItem.STATUS_REJECT + ") and t.system_type='knowledge' and ti.deleted=" + TaskItem.NO_DELETED;
        String calculateUnfinishedNumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  (ti.status=" + TaskItem.STATUS_NOT_COMMIT + "  or ti.status=" + TaskItem.STATUS_REJECT + ") and t.system_type='calc' and ti.deleted=" + TaskItem.NO_DELETED;
        String flowChartUnfinishedNumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  (ti.status=" + TaskItem.STATUS_NOT_COMMIT + "  or ti.status=" + TaskItem.STATUS_REJECT + ") and t.system_type='flowchart' and ti.deleted=" + TaskItem.NO_DELETED;

        int knowledgeUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeUnfinishedNumSql).size();
        int calculateUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(calculateUnfinishedNumSql).size();
        int flowChartUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartUnfinishedNumSql).size();

        String knowledgeFinishedNumSql = "select * from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  ti.status=" + TaskItem.STATUS_COMMIT + " and t.system_type='knowledge' and ti.deleted=" + TaskItem.NO_DELETED;
        String calculateFinishedNumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  ti.status=" + TaskItem.STATUS_COMMIT + " and t.system_type='calc' and ti.deleted=" + TaskItem.NO_DELETED;
        String flowChartFinishedNumSql = "select *  from task_item ti LEFT JOIN task t on ti.task_id=t.id  where   ti.editor='" + currentUserEnName + "'" + " and  ti.status=" + TaskItem.STATUS_COMMIT + " and t.system_type='flowchart' and ti.deleted=" + TaskItem.NO_DELETED;
        int knowledgeFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeFinishedNumSql).size();
        int calculateFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(calculateFinishedNumSql).size();
        int flowChartFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartFinishedNumSql).size();
        int knowledgeProgress = 0;
        int calculateProgress = 0;
        int flowChartProgress = 0;
        if (knowledgeFinishedNum > 0 && knowledgeSum > 0) {
            knowledgeProgress = knowledgeFinishedNum * 100 / knowledgeSum;
        }
        if (calculateFinishedNum > 0 && calculateSum > 0) {
            calculateProgress = calculateFinishedNum * 100 / calculateSum;
        }
        if (flowChartFinishedNum > 0 && flowChartSum > 0) {
            flowChartProgress = flowChartFinishedNum * 100 / flowChartSum;
        }

        map.put("knowledgeSum", knowledgeSum);
        map.put("calculateSum", calculateSum);
        map.put("flowChartSum", flowChartSum);
        map.put("knowledgeUnfinishedNum", knowledgeUnfinishedNum);
        map.put("calculateUnfinishedNum", calculateUnfinishedNum);
        map.put("flowChartUnfinishedNum", flowChartUnfinishedNum);
        map.put("knowledgeFinishedNum", knowledgeFinishedNum);
        map.put("calculateFinishedNum", calculateFinishedNum);
        map.put("flowChartFinishedNum", flowChartFinishedNum);
        map.put("knowledgeProgress", knowledgeProgress);
        map.put("calculateProgress", calculateProgress);
        map.put("flowChartProgress", flowChartProgress);
        return map;
    }

    @ApiOperation(value = "selectAuditRemindPageNum", notes = "我的审核数据接口")
    @GetMapping("selectAuditRemindPageNum")
    public Map<String, Object> selectAuditRemindPageNum(String system) throws Exception {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        Map<String, Object> map = new HashMap<>();
        switch (system) {
            case "un_reviewed": {
                String knowledgeSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                        "AND t.system_type='knowledge' AND (( ti.first_auditor ='" + currentUserEnName + "' && (ti. STATUS = 1)) " +
                        "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4)) " +
                        "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                String calcSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                        "AND t.system_type='calc' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 1)) " +
                        "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4)) " +
                        "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                String flowChartSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                        "AND t.system_type='flowchart' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 1)) " +
                        "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4)) " +
                        "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                int knowledgeTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
                int calculateTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(calcSql).size();
                int flowChartTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();
                map.put("knowledgeTaskItemNum", knowledgeTaskItemNum);
                map.put("calculateTaskItemNum", calculateTaskItemNum);
                map.put("flowChartTaskItemNum", flowChartTaskItemNum);
                break;
            }
            case "audit_Reject": {
                String knowledgeSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 AND t.system_type='knowledge' AND (( ti.first_auditor = '" + currentUserEnName + "' && ( ti. STATUS = 5)) || (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 7))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                String calcSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 AND t.system_type='calc' AND (( ti.first_auditor = '" + currentUserEnName + "' && ( ti. STATUS = 5)) || (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 7))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                String flowChartSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 AND t.system_type='flowchart' AND (( ti.first_auditor = '" + currentUserEnName + "' && ( ti. STATUS = 5)) || (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 7))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
                int knowledgeTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
                int calculateTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(calcSql).size();
                int flowChartTaskItemNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();
                map.put("knowledgeTaskItemNum", knowledgeTaskItemNum);
                map.put("calculateTaskItemNum", calculateTaskItemNum);
                map.put("flowChartTaskItemNum", flowChartTaskItemNum);
                break;
            }
            default: {
                throw new Exception("未知的知识体系类型：" + system);
            }
        }
        return map;
    }

    @ApiOperation(value = "selectAuditSum", notes = "工作台查我的审核条数接口")
    @GetMapping("selectAuditSum")
    public Map<String, Object> selectAuditSum() {
        Map<String, Object> map = new HashMap<>();
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        String knowledgeSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='knowledge' AND ( ti.first_auditor = '" + currentUserEnName + "' || ti.second_auditor = '" + currentUserEnName + "' || ti.third_auditor = '" + currentUserEnName + "')";
        String calcSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='calc' AND ( ti.first_auditor = '" + currentUserEnName + "' || ti.second_auditor = '" + currentUserEnName + "' || ti.third_auditor = '" + currentUserEnName + "')";
        String flowChartSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='flowchart' AND ( ti.first_auditor = '" + currentUserEnName + "' || ti.second_auditor = '" + currentUserEnName + "' || ti.third_auditor = '" + currentUserEnName + "')";

        int knowledgeAuditSum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeSql).size();
        int calculateAuditSum = taskItemService.getBaseMapper().executeMultiSelect(calcSql).size();
        int flowChartAuditSum = taskItemService.getBaseMapper().executeMultiSelect(flowChartSql).size();

        String knowledgeUnfinishedSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='knowledge' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 1 || ti. STATUS = 5)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4 || ti. STATUS = 7)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6 ))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        String calculateUnfinishedSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='calc' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 1 || ti. STATUS = 5)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4 || ti. STATUS = 7)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6 ))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        String flowChartUnfinishedSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='flowchart' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 1 || ti. STATUS = 5)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 4 || ti. STATUS = 7)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 6 ))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        int knowledgeAuditUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeUnfinishedSql).size();
        int calculateAuditUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(calculateUnfinishedSql).size();
        int flowChartAuditUnfinishedNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartUnfinishedSql).size();

        String knowledgeFinishedSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='knowledge' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        String calculateFinishedSql = "SELECT * FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='calc' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        String flowChartFinishedSql = "SELECT *  FROM task_item ti LEFT JOIN task t ON ti.task_id = t.id WHERE ti.deleted = 0 " +
                "AND t.system_type='flowchart' AND (( ti.first_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.second_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2)) " +
                "|| (ti.third_auditor = '" + currentUserEnName + "' && (ti. STATUS = 2))) AND ti. STATUS IN (1, 2, 3, 4, 5, 6, 7)";
        int knowledgeAuditFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(knowledgeFinishedSql).size();
        int calculateAuditFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(calculateFinishedSql).size();
        int flowChartAuditFinishedNum = taskItemService.getBaseMapper().executeMultiSelect(flowChartFinishedSql).size();
        int knowledgeAuditProgress = 0;
        int calculateAuditProgress = 0;
        int flowChartAuditProgress = 0;
        if (knowledgeAuditFinishedNum > 0 && knowledgeAuditSum > 0) {
            knowledgeAuditProgress = knowledgeAuditFinishedNum * 100 / knowledgeAuditSum;
        }
        if (calculateAuditFinishedNum > 0 && calculateAuditSum > 0) {
            calculateAuditProgress = calculateAuditFinishedNum * 100 / calculateAuditSum;
        }
        if (flowChartAuditFinishedNum > 0 && flowChartAuditSum > 0) {
            flowChartAuditProgress = flowChartAuditFinishedNum * 100 / flowChartAuditSum;
        }
        map.put("knowledgeAuditSum", knowledgeAuditSum);
        map.put("calculateAuditSum", calculateAuditSum);
        map.put("flowChartAuditSum", flowChartAuditSum);
        map.put("knowledgeAuditUnfinishedNum", knowledgeAuditUnfinishedNum);
        map.put("calculateAuditUnfinishedNum", calculateAuditUnfinishedNum);
        map.put("flowChartAuditUnfinishedNum", flowChartAuditUnfinishedNum);
        map.put("knowledgeAuditFinishedNum", knowledgeAuditFinishedNum);
        map.put("calculateAuditFinishedNum", calculateAuditFinishedNum);
        map.put("flowChartAuditFinishedNum", flowChartAuditFinishedNum);
        map.put("knowledgeAuditProgress", knowledgeAuditProgress);
        map.put("calculateAuditProgress", calculateAuditProgress);
        map.put("flowChartAuditProgress", flowChartAuditProgress);
        return map;

    }

    @ApiOperation(value = "selectRoleName", notes = "工作台查询角色名称接口")
    @GetMapping("selectRoleName")
    public  List<Role> selectRoleName() {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        User user = userService.getUserByUserName(currentUserEnName);
        List<Role> roleList = roleService.lambdaQuery()
                .in(Role::getId, user.getRoleIds()).list();
        return roleList;
    }

    @ApiOperation(value = "selectEditAndSchemeTable", notes = "工作台查询我的编辑数量接口")
    @GetMapping("selectEditAndSchemeTable")
    public List<TaskItem> selectEditAndSchemeTable(String system) throws Exception {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();

        List<TaskItem> taskItems;
        switch (system) {
            case "newTask": {
                String editSql = "select *,'1' as markStatus from task where id in(select task_id from task_item where status!=" + TaskItem.STATUS_RELEASED + " and editor='" + currentUserEnName + "') and status = " + Task.STATUS_STARTED + " order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(editSql);
                taskItems.forEach(taskItem -> taskItem.setName(taskItem.getName() + "-(编辑)"));
                String auditSql = "select *,'2' as markStatus from task where id in(select task_id from task_item where status!=8 and (first_auditor='" + currentUserEnName + "' ||second_auditor='" + currentUserEnName + "' ||third_auditor='" + currentUserEnName + "')) and status = 1 order by updated_time desc";
                List<TaskItem> auditList = taskItemService.getBaseMapper().executeMultiSelect(auditSql);
                auditList.forEach(taskItem -> taskItem.setName(taskItem.getName() + "-(审核)"));
                taskItems.addAll(auditList);
                taskItems.sort(Comparator.comparing(TaskItem::getUpdatedTime).reversed());
                break;
            }
            case "rejectTask": {
                String sql = "SELECT * FROM task_item  WHERE deleted = 0  AND ((first_auditor ='" + currentUserEnName + "' && STATUS = 5 ) || (second_auditor = '" + currentUserEnName + "' &&  STATUS = 7) || third_auditor = '" + currentUserEnName + "' || editor = '" + currentUserEnName + "')AND  STATUS IN ( 3,  5, 7) order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(sql);
                break;
            }
            case "pressTask": {
                String sql = "SELECT * FROM task_item  WHERE deleted = 0 and status != " + TaskItem.STATUS_RELEASED + " AND ((first_auditor = '" + currentUserEnName + "' && (STATUS = 1 || STATUS = 5 )) || (second_auditor = '" + currentUserEnName + "' && (STATUS = 4 ||  STATUS = 7 )) || (third_auditor = '" + currentUserEnName + "' &&  STATUS = 6) ||(editor ='" + currentUserEnName + "'  && (status = 0|| status = 3))) and press=1 order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(sql);

                break;
            }
            default: {
                throw new Exception("未知的知识体系类型：" + system);
            }
        }
        return taskItems;

    }

    @ApiOperation(value = "selectAuditTableData", notes = "工作台查询我的审核数量接口")
    @GetMapping("selectAuditTableData")
    public List<TaskItem> selectAuditTableData(String system) throws Exception {
        String currentUserEnName = PMPHAppUtil.getCurrentUserEnName();
        List<TaskItem> taskItems;
        switch (system) {
            case "newTask": {
                String editSql = "select *,'1' as markStatus from task where id in(select task_id from task_item where status!=" + TaskItem.STATUS_RELEASED + " and editor='" + currentUserEnName + "') and status = " + Task.STATUS_STARTED + " order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(editSql);
                taskItems.forEach(taskItem -> taskItem.setName(taskItem.getName() + "-(编辑)"));
                String auditSql = "select *,'2' as markStatus from task where id in(select task_id from task_item where status!=8 and (first_auditor='" + currentUserEnName + "' ||second_auditor='" + currentUserEnName + "' ||third_auditor='" + currentUserEnName + "')) and status = 1 order by updated_time desc";
                List<TaskItem> auditList = taskItemService.getBaseMapper().executeMultiSelect(auditSql);
                auditList.forEach(taskItem -> taskItem.setName(taskItem.getName() + "-(审核)"));
                taskItems.addAll(auditList);
                taskItems.sort(Comparator.comparing(TaskItem::getUpdatedTime).reversed());
                break;
            }
            case "rejectTask": {
                String sql = "SELECT * FROM task_item WHERE deleted = 0 AND (( first_auditor = '" + currentUserEnName + "' &&  STATUS = 5 ) || (second_auditor = '" + currentUserEnName + "' && STATUS = 7 )||(editor ='" + currentUserEnName + "'&&status= " + TaskItem.STATUS_REJECT + " )) order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(sql);
                break;
            }
            case "pressTask": {
                String sql = "SELECT * FROM task_item  WHERE deleted = 0 and status != " + TaskItem.STATUS_RELEASED + " AND ((first_auditor = '" + currentUserEnName + "' && (STATUS = 1 || STATUS = 5 )) || (second_auditor = '" + currentUserEnName + "' && (STATUS = 4 ||  STATUS = 7 )) || (third_auditor = '" + currentUserEnName + "' &&  STATUS = 6) || (editor ='" + currentUserEnName + "' && (status = 0 ||status = 3) )) and press=1 order by updated_time desc";
                taskItems = taskItemService.getBaseMapper().executeMultiSelect(sql);

                break;
            }
            default: {
                throw new Exception("未知的知识体系类型：" + system);
            }
        }
        return taskItems;

    }

}

